package unimelb.distSystems.game.elements;

import java.text.DecimalFormat;

import unimelb.distSystems.project2.Configurations;

import com.jme3.asset.AssetManager;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Node;
import com.jme3.system.Timer;
import com.jme3.ui.Picture;
import com.jme3.font.BitmapText;

public class GUIPanel {
	BitmapText speed;
	DecimalFormat df=new DecimalFormat("########");
	DecimalFormat df_time=new DecimalFormat( "###.##");
	Picture pointer;
	float lastSpeed=0;
	BitmapText timeUsed;
	BitmapText timeCounter;
	BitmapText result;
	
	int panelWidth=859;
	int panelHeight=369;
	
	public void setup(AssetManager assetManager, Node guiNode)
	{
		guiNode.detachAllChildren();
		
		// back ground of speed panel
		Picture p = new Picture("Picture");
        p.move(0, 0, -1); // make it appear behind stats view
        float panelLocalX=Configurations.gameScreenWidth-panelWidth/2.5f;
        p.setPosition(panelLocalX, 0);
        p.setWidth(panelWidth/2.5f);
        p.setHeight(panelHeight/2.5f);
        p.setImage(assetManager, "Models/panel.png", true);
        guiNode.attachChild(p);
        
		pointer = new Picture("Picture");
		pointer.move(0, 0, -1); // make it appear behind stats view
		pointer.setPosition(panelLocalX+100, 48);
		pointer.setWidth(75/2.5f);
		pointer.setHeight(200/2.5f);
		pointer.setImage(assetManager, "Models/pointer.png", true);
		pointer.rotate(0, 0, 2.40f);
        guiNode.attachChild(pointer);
        
        speed = new BitmapText(assetManager.loadFont("Interface/Fonts/Default.fnt"), false);
        speed.setSize(27);
        speed.setColor(ColorRGBA.White);
        speed.setLocalTranslation(panelLocalX+200, speed.getLineHeight(), 0);
        guiNode.attachChild(speed);
        
        timeUsed = new BitmapText(assetManager.loadFont("Interface/Fonts/Default.fnt"), false);
        timeUsed.setSize(38);
        timeUsed.setColor(ColorRGBA.White);
        timeUsed.setLocalTranslation(Configurations.gameScreenWidth-200, Configurations.gameScreenHeight, 0);
        guiNode.attachChild(timeUsed);
        
        timeCounter = new BitmapText(assetManager.loadFont("Interface/Fonts/Default.fnt"), false);
        timeCounter.setSize(120);
        timeCounter.setColor(ColorRGBA.Yellow);
        timeCounter.setLocalTranslation(Configurations.gameScreenWidth/2-120, Configurations.gameScreenHeight/2+200, 0);
        guiNode.attachChild(timeCounter);
        
        result = new BitmapText(assetManager.loadFont("Interface/Fonts/Default.fnt"), false);
        result.setSize(120);
        result.setColor(ColorRGBA.Yellow);
        result.setLocalTranslation(Configurations.gameScreenWidth/2-250, Configurations.gameScreenHeight/2+150, 0);
        guiNode.attachChild(result);
	}
	
	public void setSpeed(float speed)
	{
		float speedChange=speed-lastSpeed;
		pointer.rotate(0, 0, -(float)(Math.PI*speedChange/160));
		lastSpeed=speed;
		if(speed<0)
			this.speed.setText(df.format(-speed));
		else
			this.speed.setText(df.format(speed));
	}
	
	
	public void showTimeUsed(Timer timer)
	{
		if(timer.getTime()/1000f-5>1)
		{
			if(timer.getTime()/1000f-5>1&&timer.getTime()/1000f-5<2)
			{
				timeCounter.setText("");
			}
			timeUsed.setText("Time:"+df_time.format(timer.getTime()/1000f-3));
		}
		else if( timer.getTime()/1000f-5<0.5 )
		{
			timeCounter.setText(df.format(-(timer.getTime()/1000f-6)));
		}
		else
		{
			timeCounter.setText("GO!");
		}
	}
	
	public void showRacingResult(String who_text)
	{
		result.setText(who_text);
	}
}
